package com.bjsxt.common.controller;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.common.util.ConstatFinalUtil;
import com.bjsxt.common.util.DateFormatUtil;
import com.bjsxt.common.util.EncryptUtil;
import com.bjsxt.common.util.PageInfoUtil;
import com.bjsxt.common.util.RegexUtil;
import com.bjsxt.common.util.VerifyCodeUtils;

public class BaseController {
	@Resource
	protected EncryptUtil encryptUtil;
	@Resource
	protected RegexUtil regexUtil;
	@Resource
	protected DateFormatUtil dateFormatUtil;
	protected String info;

	// 生成验证码
	@RequestMapping("/code.htm")
	public String code(HttpServletRequest request, HttpServletResponse response) {
		HttpSession session = request.getSession();
		ConstatFinalUtil.LOGGER.info("---- code---");
		String code = this.regexUtil.randString(4);
		try {
			VerifyCodeUtils.outputImage(100, 50, response.getOutputStream(), code);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		session.setAttribute("code", code);

		return null;
	}

	// 生成分页对象
	protected PageInfoUtil proccedPageInfo(HttpServletRequest request) {
		PageInfoUtil pageInfoUtil = new PageInfoUtil();
		String currentPage = request.getParameter("currentPage");
		String pageSize = request.getParameter("pageSize");
		try {
			pageInfoUtil.setCurrentPage(Integer.valueOf(currentPage));
			pageInfoUtil.setPageSize(Integer.valueOf(pageSize));
		} catch (NumberFormatException e) {
		
		}
		return pageInfoUtil;
	}
	//拼装JSON JUI框架需要JSON
	protected JSONObject returnJuiJSON(HttpServletRequest request, JSONObject jsonObject){
		//JSON中的其他信息从request中获取（JSP页面上）
		String navTabId=request.getParameter("navTabId");
		String rel=request.getParameter("rel");
		String callbackType=request.getParameter("callbackType");
		String forwardUrl=request.getParameter("forwardUrl");
		String confirmMsg=request.getParameter("confirmMsg");
		if(navTabId == null)
		{
			navTabId = "" ; 
		}
		if(rel == null)
		{
			rel = "" ; 
		}
		if(callbackType == null)
		{
			callbackType = "" ; 
		}
		if(forwardUrl == null)
		{
			forwardUrl = "" ; 
		}
		if(confirmMsg == null)
		{
			confirmMsg = "" ; 
		}
		JSONObject resultJSON=new JSONObject();
		
		if(jsonObject.get("code")=="1"){
		resultJSON.put("statusCode","300");
		}if(jsonObject.get("code")=="0"){
		resultJSON.put("statusCode","200");
		}
		
		resultJSON.put("message", jsonObject.get("info"));
		resultJSON.put("navTabId", navTabId);
		resultJSON.put("rel", rel);
		resultJSON.put("callbackType", callbackType);
		resultJSON.put("forwardUrl", forwardUrl);
		resultJSON.put("confirmMsg", confirmMsg);
		return resultJSON;
	}
	
}
